package com.cosina.game.robodefence.ui;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

import com.cosina.game.robodefence.R;
import com.cosina.game.robodefence.dao.EnemyDao;
import com.cosina.game.robodefence.data.EnemyData;
import com.cosina.game.robodefence.data.TowerData;
import com.cosina.game.robodefence.util.BitmapUtil;
import com.cosina.game.robodefence.util.World;

public class ImageManager {
	
	public static Bitmap[][] towers;;
	public static Bitmap[][] enemey;
	public static Bitmap heart;
	
	
	public static void init(Resources res){
		initTower(res);
		initEnemy(res);
		heart = BitmapFactory.decodeResource(res, R.drawable.heart);
	}
	
	private static void initEnemy(Resources res) {
		enemey = new Bitmap[EnemyData.KIND_COUNT][0];
		for(int i=0; i<EnemyData.KIND_COUNT; i++){
			Bitmap bitmap = BitmapFactory.decodeResource(res, EnemyDao.getResId(i));
			enemey[i] = BitmapUtil.cut(bitmap, EnemyDao.getMotionCount(i)*4, World.H);
		}
	}
	
	public static Bitmap getEnemyFrame(String direction, int kind){
		return enemey[kind][getDirectionIndex(direction)*EnemyDao.getMotionCount(kind) + MotionManager.getFrame(kind)];
	}
	
	private static int getDirectionIndex(String dirString){
		if(dirString.equals(World.LEFT)){
			return 2;
		}
		if(dirString.equals(World.RIGHT)){
			return 0;
		}
		if(dirString.equals(World.TOP)){
			return 1;
		}
		if(dirString.equals(World.BOTTOM)){
			return 3;
		}
		return 5;
	}
	
	
	private static void initTower(Resources res){
		int[][] loadImageInfo = {
				{ R.drawable.gun_tower,17,TowerData.GUNONE},
				{ R.drawable.gun_tower2,17,TowerData.GUNTWO},
				{ R.drawable.gun_tower3,17,TowerData.GUNTHREE},
				{ R.drawable.aa_tower,17,TowerData.ANTIAIRONE},
				{ R.drawable.aa_tower2,17,TowerData.ANTIAIRTWO},
				{ R.drawable.flame_tower,17,TowerData.FLAMEONE},
				{ R.drawable.flame_tower2,17,TowerData.FLAMETWO},
				{ R.drawable.rocket_tower,17,TowerData.ROCKETONE},
				{ R.drawable.rocket_tower2,17,TowerData.ROCKETTWO},
				{ R.drawable.rocket_tower3,17,TowerData.ROCKETTHREE},
				
				{ R.drawable.sam_tower,17,TowerData.SAMONE},
				{ R.drawable.sam_tower2,17,TowerData.SAMTWO},
				{ R.drawable.ice_tower,5,TowerData.ICEONE},
				{ R.drawable.ice_tower2,5,TowerData.ICETWO},
				{ R.drawable.ice_tower3,5,TowerData.ICETHREE},
				
				{ R.drawable.mortar,2,TowerData.MORTAR},
				{ R.drawable.artillery,2,TowerData.ARTILERY},
				
				{ R.drawable.sell,1,TowerData.SELL},
		};
		
		towers = new Bitmap[loadImageInfo.length + 1][17];
		
		for(int i=0; i<loadImageInfo.length; i++){
			loadTowerImage(res,loadImageInfo[i][0],loadImageInfo[i][1],loadImageInfo[i][2]);
		}
	}
	
	private static void loadTowerImage(Resources res, int resId,int part, int typeId){
		Bitmap bitmap = BitmapFactory.decodeResource(res, resId);
		towers[typeId] = BitmapUtil.cut(bitmap, part, World.H);
	}
	
	public static void drawTower(int kind, int direction, Canvas canvas, int locationx, int locationy){
		canvas.drawBitmap(towers[kind][0], locationx, locationy, PaintSuite.p4Text);
		if(towers[kind].length == 1){
			return ;
		}
		Bitmap upPart = towers[kind][1];
		if(towers[kind].length == 17){
			upPart = towers[kind][direction + 1];
		}
		canvas.drawBitmap(upPart, locationx, locationy-14, PaintSuite.p4Text);
	}
}
